package com.jpa.yuan.control;

import com.jpa.yuan.mapper.Userrepo;
import com.jpa.yuan.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.*;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
public class a {
    @Autowired
    Userrepo u;

    @GetMapping("/")
    private String geta() {
        return "a";
    }

    @GetMapping("b")
    public User insert() {
        User user = new User();
        user.setLastName("test_05").setAge(5).setCreateTime(new Date());
        u.save(user);
        return user;
    }

    @GetMapping("fz")
    private String gfz() {
   /*     User user = new User().setLastName("test_05");
        Optional<User> one = u.findOne(Example.of(user));
        Specification<User> userSpecification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                Path<Object> name = root.get("lastName");
                Predicate equal = cb.equal(name, user.getLastName());
                return equal;
            }
        };
        Optional<User> one1 = u.findOne(userSpecification);*/
        return "one1";
    }


    @GetMapping("c/{id}")
    private User getuser(@PathVariable int id) {
        User user = null;
        Example<User> of = Example.of(new User().setId(id));
        Optional<User> one = u.findOne(of);
        Optional<User> byId = u.findById(4);
        log.info("one", one);
        if (byId.isPresent()) {
            user = byId.get();
            log.info("user", user);
            return user;
        }
        return user;
    }

    @GetMapping("d")
    public User getbyname() {
        User li = u.findByCreateTimeBefore(new Date());
        return li;
    }

    @GetMapping("e")
    public User gestbyname() {
        List<User> li1 = u.findByAgeAndAddress("test_01");
        User li = li1.get(0);
        return li;
    }

    @GetMapping("80")
    public String get81() {
        return "80";
    }

    public int getff(@Param(value = "int") int a, String f) {
        return 3;
    }
}
